package com.letfox.ad.controller.ad;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.letfox.ad.controller.BaseController;
import com.letfox.ad.model.ADColScre;
import com.letfox.ad.service.ColScreService;
import com.letfox.common.enums.ResponseEnum;
import com.letfox.common.model.ResultResponse;

@Controller
@RequestMapping(value = "/colScre")
public class ColScreController extends BaseController {

	@Autowired
	private ColScreService colScreService;

	@RequestMapping(value = "/test", method = RequestMethod.GET)
	@ResponseBody
	public void getRedis() {
		String json = null;
		try {
			colScreService.writeRedisJob();
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
	}

	
	
	@RequestMapping(value = "/phys", method = RequestMethod.GET)
	@ResponseBody
	public void getPhys() {
		String json = null;
		try {
			json = colScreService.getPhysName();
			response.getWriter().print(json);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
	}

	@RequestMapping(value = "/msgUrl", method = RequestMethod.POST)
	@ResponseBody
	public ResultResponse getMsgUrlColScre(String request_json) {
		ResultResponse resultResponse = new ResultResponse();
		try {

			JSONObject jsonObject = JSONObject.fromObject(request_json);
			resultResponse = colScreService.getColScreUrl(jsonObject
					.getString("screen_id"));
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			String json="{\"ret\":\"1\",\"msg\":\"系统错误\"}";
			resultResponse.setData(json);
			resultResponse.setStatusCode(ResponseEnum.R500.getCode());
			resultResponse.setStatusMessage(ResponseEnum.R500.getMessage());
		}
		return resultResponse;
	}

	@RequestMapping(value = "/create", method = RequestMethod.POST)
	@ResponseBody
	public ResultResponse getNewAdScreen(ADColScre formFile) {
		ResultResponse resultResponse = new ResultResponse();
		try {
			resultResponse = colScreService.insertAdScreen(formFile);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			resultResponse.setStatusCode(ResponseEnum.R500.getCode());
			resultResponse.setStatusMessage(ResponseEnum.R500.getMessage());
		}
		return resultResponse;
	}

	@RequestMapping(value = "/createPhys", method = RequestMethod.POST)
	@ResponseBody
	public ResultResponse getNewPhysScreen(String request_json) {
		ResultResponse resultResponse = new ResultResponse();
		try {
			resultResponse = colScreService.getNewPhysScreen(request_json);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			String json = "{" + "\"" + ResponseEnum.R500.getCode()
					+ "\",\"system erro\"}";
			resultResponse.setData(json);
			resultResponse.setStatusCode(ResponseEnum.R500.getCode());
			resultResponse.setStatusMessage(ResponseEnum.R500.getMessage());
		}
		return resultResponse;
	}

	@RequestMapping(value = "/censusScreen", method = RequestMethod.POST)
	@ResponseBody
	public void censusScreenAd(String request_json) {
		ResultResponse resultResponse = new ResultResponse();
		try {
			resultResponse = colScreService.censusScreenAd(request_json);
			response.getWriter().print(resultResponse.getData());
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			String json = "{" + "\"" + ResponseEnum.R500.getCode()
					+ "\",\"system erro\"}";
			resultResponse.setData(json);
			resultResponse.setStatusCode(ResponseEnum.R500.getCode());
			resultResponse.setStatusMessage(ResponseEnum.R500.getMessage());
		}
	}

	@RequestMapping(value = "/allADPlan", method = RequestMethod.POST)
	@ResponseBody
	public void getADPlans() {
		String json = null;
		try {
			json = colScreService.getAllADPlan();
			response.getWriter().print(json);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
	}

	@RequestMapping(value = "/censusPlan", method = RequestMethod.POST)
	@ResponseBody
	public ResultResponse censusScreenPlan(String request_json) {
		ResultResponse resultResponse = new ResultResponse();
		try {
			resultResponse = colScreService.getAdRecord(request_json);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			String json = "{" + "\"" + ResponseEnum.R500.getCode()
					+ "\",\"system erro\"}";
			resultResponse.setData(json);
			resultResponse.setStatusCode(ResponseEnum.R500.getCode());
			resultResponse.setStatusMessage(ResponseEnum.R500.getMessage());
		}
		return resultResponse;
	}

	@RequestMapping(value = "/queryList", method = RequestMethod.POST)
	@ResponseBody
	public ResultResponse queryVaguePlan(String request_json) {
		ResultResponse resultResponse = new ResultResponse();
		try {
			resultResponse = colScreService.queryPlanList(request_json);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			String json = "{" + "\"" + ResponseEnum.R500.getCode()
					+ "\",\"system erro\"}";
			resultResponse.setData(json);
			resultResponse.setStatusCode(ResponseEnum.R500.getCode());
			resultResponse.setStatusMessage(ResponseEnum.R500.getMessage());
		}
		return resultResponse;
	}

	@RequestMapping(value = "/adscreen_info", method = RequestMethod.POST)
	@ResponseBody
	public ResultResponse adscreen_info(String adId) {
		ResultResponse resultResponse = new ResultResponse();
		try {
			resultResponse = colScreService.queryScreenInfo(adId);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			String json = "{" + "\"" + ResponseEnum.R500.getCode()
					+ "\",\"system erro\"}";
			resultResponse.setData(json);
			resultResponse.setStatusCode(ResponseEnum.R500.getCode());
			resultResponse.setStatusMessage(ResponseEnum.R500.getMessage());
		}
		
		return resultResponse;
	}
	
	@RequestMapping(value = "/submitAudit", method = RequestMethod.POST)
	@ResponseBody
	public ResultResponse submitAudit(String request_json) {
		ResultResponse resultResponse = new ResultResponse();
		try {
			resultResponse = colScreService.updateScreenInfo(request_json);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			String json = "{" + "\"" + ResponseEnum.R500.getCode()
					+ "\",\"system erro\"}";
			resultResponse.setData(json);
			resultResponse.setStatusCode(ResponseEnum.R500.getCode());
			resultResponse.setStatusMessage(ResponseEnum.R500.getMessage());
		}
		return resultResponse;
	}

}
